/**
 * @project_name staff_vehicle
 * @package_name com.ssh.dao
 * @desc TODO
 * @param
 * @author jack
 * @date 2013-4-23
 * @time 下午4:08:36
 */
package com.ssh.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.ssh.entity.AskForleave;
import com.ssh.model.pdata.AskForLeaveMData;

/**
 * @author jack
 * @员工请假表
 */
public class AskForleaveDAOImpl implements AskForleaveDAOI {
	private SessionFactory sessionFactory;

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.AskForleaveDAOI#addAskFL(com.ssh.entity.AskForleave)
	 */
	@Override
	public AskForleave addAskFL(AskForleave askFL) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().save(askFL);
		} catch (Exception e) {
			return null;
		}

		return askFL;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.AskForleaveDAOI#addAskFL(com.ssh.entity.AskForleave[])
	 */
	@Override
	public int addAskFL(AskForleave... AskFLs) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.AskForleaveDAOI#deleteAskFL(java.lang.String[])
	 */
	@Override
	public int deleteAskFL(String... strings) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.AskForleaveDAOI#deleteAskFLById(java.lang.String)
	 */
	@Override
	public AskForleave deleteAskFLById(String AskFLId) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * @return the sessionFactory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.AskForleaveDAOI#queryAskFL(java.lang.String[])
	 */
	@Override
	public List<AskForleave> queryAskFL(String... strings) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.AskForleaveDAOI#queryAskFLByExtra(java.lang.String)
	 */
	@Override
	public List<AskForleave> queryAskFLByExtra(String extra) {
		// TODO Auto-generated method stub
		List<AskForleave> askFLList ;
		try{
			String hql = "from AskForleave as askForleave" +
					" where askForleave.staffId = ? " +
					"or where askForleave.reviewerId = ? " ;
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setString(0, extra);
			query.setString(1, extra);
			askFLList = query.list();
		}
		catch(Exception e){
			return null ;
		}
		return askFLList ;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.AskForleaveDAOI#queryAskFLById(java.lang.String)
	 */
	@Override
	public AskForleave queryAskFLById(String AskFLId) {
		// TODO Auto-generated method stub
		AskForleave askFL ;
		try{
			String hql = "from AskForleave as askForleave" +
					" where askForleave.staffId = ? " ;
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setString(0, AskFLId);
			askFL = (AskForleave) query.uniqueResult();
		}
		catch(Exception e){
			return null ;
		}
		return askFL ;
	}

	@Override
	public AskForleave queryById(int id) {
		// TODO Auto-generated method stub
		AskForleave askFL ;
		try{
			String hql = "from AskForleave as askForleave" +
					" where askForleave.idaskForleave =:id " ;
			Query query = sessionFactory.getCurrentSession().createQuery(hql).setParameter("id", id);
			askFL = (AskForleave) query.uniqueResult();
		}
		catch(Exception e){
			return null ;
		}
		return askFL ;
	}
	
	/**
	 * @param sessionFactory
	 *            the sessionFactory to set
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.AskForleaveDAOI#updateAskFL(com.ssh.entity.AskForleave)
	 */
	@Override
	public AskForleave updateAskFL(AskForleave AskFL) {
		// TODO Auto-generated method stub
		try{
			sessionFactory.getCurrentSession().update(AskFL);
		}
		catch(Exception e){
			return null;
			}
		return AskFL ;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.ssh.dao.AskForleaveDAOI#updateAskFL(com.ssh.entity.AskForleave[])
	 */
	@Override
	public int updateAskFL(AskForleave... AskFLs) {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.AskForleaveDAOI#queryMyAFL(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<AskForleave> queryMyAFL(String staffId) {
		// TODO Auto-generated method stub
		String hql = "from AskForleave as a where a.staffId=:staffId";
		return sessionFactory.getCurrentSession().createQuery(hql).setParameter("staffId", staffId).list();
	}

	@Override
	public List<AskForLeaveMData> queryAllAskForLeave() {
		// TODO Auto-generated method stub
		String hql ="select new com.ssh.model.pdata.AskForLeaveMData(afl,if,sp) from AskForleave afl,Staff if,Staff sp where afl.staffId=if.staffId and afl.reviewerId=sp.staffId";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}

	@Override
	public List<AskForLeaveMData> queryAllAskForLeave(int state) {
		// TODO Auto-generated method stub
		String hql ="select new com.ssh.model.pdata.AskForLeaveMData(afl,if,sp) " +
				"from AskForleave afl,Staff if,Staff sp " +
				"where afl.staffId=if.staffId and afl.reviewerId=sp.staffId and afl.state=:state";
		Query query = sessionFactory.getCurrentSession().createQuery(hql).setParameter("state", state);
		return query.list();
	}

	@Override
	public List<AskForLeaveMData> queryOneAskForLeave(String idWhoAskForLeave) {
		// TODO Auto-generated method stub
		String hql ="select new com.ssh.model.pdata.AskForLeaveMData(afl,if,sp) " +
				"from askForleave afl,Staff if,Staff sp " +
				"where afl.staffId if.staffId and afl.reviewerId=sp.staffId and if.staffId=:idWhoAskForLeave";
		Query query = sessionFactory.getCurrentSession().createQuery(hql).setParameter("idWhoAskForLeave", idWhoAskForLeave);
		return query.list();
	}

	@Override
	public List<AskForLeaveMData> queryOneAskForLeave(String idWhoAskForLeave,int state) {
		// TODO Auto-generated method stub
		String hql ="select new com.ssh.model.pdata.AskForLeaveMData(afl,if,sp) " +
				"from askForleave afl,Staff if,Staff sp " +
				"where afl.staffId if.staffId and afl.reviewerId=sp.staffId " +
				" and if.staffId=:idWhoAskForLeave and afl.state=:state";
		Query query = sessionFactory.getCurrentSession().createQuery(hql).
				setParameter("idWhoAskForLeave", idWhoAskForLeave).setParameter("state", state);
		return query.list();
	}


}
